#include <project.h>
/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module file.c - File check functions |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Utils/file.c $ 
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#define VSFILE_C

#include <aclconio.h>
#include <stdio.h>
#include <applidl.h>
#include <message.h>
#include <errno.h>
#include <string.h>

/** /#define DEBUG_VSFILE*/
#ifdef DEBUG_VSFILE
#define DEBUG_95
#endif

#include <sizes.h>
#include <define.h>
#include <ui.h>
#include <msg.h>
#include <msgfuncs.h>
#include <file.h>
#include <spdebug.h>
/*-------------------------------------------------------------------------
    Function    : lnGetFileSize
    Description : returns file size in bytes, on errors return file error codes.
    Parameters  : none.
    Returns     : Size of file
    Globals     : none.
    Notes       :

    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   07/10/95 Carlos_R1  Found that this function didn't release the
                               handle before returning.  Made func close
                               file before returning.
--------------------------------------------------------------------------*/

long lnGetFileSize(const char* pchFileName) {
    long lnSeekPos;
    FHANDLE hFHandle;

    /**/pdebug(("--- lnGetFileSize ---"));/**/
    /**/pdebug(("FileName:%s", pchFileName));/**/
    hFHandle = open(pchFileName, O_RDONLY);
    lnSeekPos = lseek(hFHandle, 0L, SEEK_END);
    /**/pdebug(("--Size:%ld--", lnSeekPos));/**/
    close(hFHandle);    /* Was previously commented out */

    return(lnSeekPos);
}
/*-------------------------------------------------------------------------
    Function    : inCheckFileSystem
    Description : Check the checksum for all open files and report errors.
    Parameters  : none.
    Returns     : none
    Globals     : none
    Notes       : none

    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   06/28/95 Carlos_R1  Added to vsutil, courtesy of Jaime_S1
      2   07/10/95 Carlos_R1  Moved to DEBUG.C
--------------------------------------------------------------------------*/
int inCheckFileSystem() {
    char szFile[13];            /* File Name            */
    char szTempBuff[30];        /* Error Msg Buffer     */
    int inFileStat;         /* File Handle          */
    int inResult = FALSE;

    vdDisplayMessageFromFile(MEMORY_CHECK_MSG21, STATUS_MSG);

    /* Get the first file in the file system */
    dir_get_first(szFile);

    do          /* Loop through each file. */ {
        if (!strcmp(szFile, "DATA.EM")) /*DATA.EM always reports w/ errors */
            continue;

        inFileStat = SVC_CHECKFILE(szFile); /* Check for checksum errors   */

        if (inFileStat)     /* If file is corrupt */ {
            inResult = TRUE;
            sprintf(szTempBuff, "ERR: %d, %s", inFileStat, szFile);
            vdDisplayErrorString(szTempBuff);
        }
    } while (dir_get_next(szFile) != -1);

    return(inResult);
}

/*-------------------------------------------------------------------------
    Function    : inIdleChkFileSys
    Description : Check the checksum for all files from the Idle Engine
    Parameters  : none.
    Returns     : BRANCH_EXIT
    Globals     : none
    Notes       : none

    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   06/28/95 Carlos_R1  Added to vsutil, courtesy of Jaime_S1
      2   07/10/95 Carlos_R1  Moved to DEBUG.C
--------------------------------------------------------------------------*/

int inIdleChkFileSys() {
    inCheckFileSystem();
    return(BRANCH_EXIT);
}

/*-------------------------------------------------------------------------
 Function :     inRemoveFile
 Description :  Removes the file if it is present
 Parameters :   
 Returns :      VS_SUCCESS, VS_ERR
 Notes :
 --------------------------------------------------------------------------*/
int inRemoveFile(char* pchFName) {
    if (remove(pchFName) != VS_SUCCESS) {
        extern int errno;
        if (errno != ENOENT) {
            pdebug(("RF errno=%d,'%s'", errno, pchFName));
            return(VS_ERR);
        }
    }

    return (VS_SUCCESS);
}

#undef VSFILE_C

